package com.esm.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.esm.model.User;
import com.esm.service.FindReturnMsgService;
import com.esm.service.FindSelfInfoService;
import com.esm.serviceimpl.FindReturnMsgServiceImpl;
import com.esm.serviceimpl.FindSelfInfoServiceImpl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * @author zyj
 *
 */
public class GetReturnMsgController extends HttpServlet{
	FindReturnMsgService fs=new FindReturnMsgServiceImpl();
	FindSelfInfoService fss=new FindSelfInfoServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String lgroup=req.getParameter("lgroup");
		HttpSession session=req.getSession();
		User curUser=(User) session.getAttribute("currentUser");
		int uid=curUser.getUid();
		JSONArray returnMsg=fs.findReturnMsg(lgroup, uid);
		for(int i=0;i<returnMsg.size();i++){
			JSONObject j=returnMsg.getJSONObject(i);
			j.put("curUid", uid);
			int msguid=j.getInt("uid");
			JSONObject u=fss.findUserInfo(msguid);
			String uname=u.getString("uname");
			j.put("uname",uname);
		}
		PrintWriter pw=resp.getWriter();
		pw.write(returnMsg.toString());
	}
}
